/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.HabitacionTipoHab;


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import solucionessgh.Habitacion.HabitacionModel;
import solucionessgh.TipoHabitacion.TipoHabitacionModel;
import solucionessgh.Util.ComboInfo;
import solucionessgh.Util.DataBase;

/**
 *
 * @author roderick
 */
public class HabitacionTipoHabModel extends DataBase{
    
    private int id;
    private int habcod;
    private int thacod;
    private String habnum;
    private boolean hthact;


     public HabitacionTipoHabModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO habitacion_tipohab(habcod, thacod, habnum, hthact) VALUES (?, ?, ?,?);");
                stmt.setInt(1,this.habcod);
                stmt.setInt(2,this.thacod);
                stmt.setString(3,this.habnum);
                stmt.setBoolean(4, hthact);
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE habitacion_tipohab SET habcod=?, thacod=?, habnum=?, hthact=? WHERE habid=? ;");
                stmt.setInt(1, this.habcod);
                stmt.setInt(2,this.thacod);
                stmt.setString(3,this.habnum);
                stmt.setBoolean(4, hthact);
                stmt.setInt(5, this.id);
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean delete(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from habitacion_tipohab where habid=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT tp.tcbfec, tp.tcbcom, tp.tcbven,tp.id, m.mondesc as moncod,habid FROM tipocambio tp INNER JOIN moneda m ON m.id=tp.moncod;");
    }

    public ResultSet finder(String var){
        return this.find("SELECT h.habdesc as habcod, th.thadesc as thacod, hab.habnum, hab.hthact, hab.habid FROM habitacion_tipohab hab "+
                         "INNER JOIN habitacion h ON h.habid = hab.habcod "+
                         "INNER JOIN tipohabitacion th ON th.thacod = hab.thacod WHERE th.thadesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT habcod, thacod, habnum, hthact, habid  FROM habitacion_tipohab WHERE habid = "+var+";");
    }

    public void setComboHabitacion(JComboBox combo){
        HabitacionModel model_tarifa = new HabitacionModel();
        ResultSet rs = model_tarifa.read();
        try {
            while ( rs.next()){
                combo.addItem(new ComboInfo(rs.getInt("habid"),rs.getString("habdesc")));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        }
    }

    public void setComboTipoHabitacion(JComboBox combo){
        TipoHabitacionModel model_habitacion = new TipoHabitacionModel();
        ResultSet rs = model_habitacion.read();
        try {
            while ( rs.next()){
                combo.addItem(new ComboInfo(rs.getInt("thacod"),rs.getString("thadesc")));
            }
        }catch (SQLException ex) {
            System.err.println(ex.toString());
        }
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getHabcod() {
        return habcod;
    }

    public void setHabcod(int habcod) {
        this.habcod = habcod;
    }

    public String getHabnum() {
        return habnum;
    }

    public void setHabnum(String habnum) {
        this.habnum = habnum;
    }

    public boolean isHthact() {
        return hthact;
    }

    public void setHthact(boolean hthact) {
        this.hthact = hthact;
    }

    public int getThacod() {
        return thacod;
    }

    public void setThacod(int thacod) {
        this.thacod = thacod;
    }
    

}
